package com.qqtech.qquan.pub.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.pub.enums.PubReportStatusDictEnum;
import com.qqtech.qquan.pub.model.PubReport;
import com.qqtech.qquan.pub.model.PubReportQuery;
import com.qqtech.qquan.pub.model.PubReportVo;
import com.qqtech.qquan.pub.service.PubReportService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBaseQuery;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserAdminService;
import com.qqtech.qquan.user.service.UserBaseService;
@Controller
@RequestMapping(value = "/pub/report")
public class PubReportController extends BaseAdminController{
	@Resource
	private UserAdminService userAdminService;
	@Resource
	private PubReportService pubReportService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<PubReport> getBaseService() {
		return pubReportService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/report/report-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(PubReportQuery report ,HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		report.setSize(Integer.valueOf(rows));
		
		Pageable pageKit = new PageKit(report.getPage(),report.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<PubReportVo> page = pubReportService.queryPageListAdmin(report, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/list2", method = RequestMethod.GET)
	public String list2() {
		return "/report/report-list2";
	}

	@RequestMapping(value = "/list2", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list2(PubReportQuery report ,HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		report.setSize(Integer.valueOf(rows));
		report.setStatus(PubReportStatusDictEnum.WAITING.getCode());//待处理
		Pageable pageKit = new PageKit(report.getPage(),report.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<PubReportVo> page = pubReportService.queryPageListAdmin(report, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		PubReportVo report = pubReportService.getById(id);
		UserBaseQuery userQuery = new UserBaseQuery();
		Pageable pageKit = new PageKit(new PageOrder(Direction.ASC, "id"));
		if(report != null){
			if(report.getCommunityId()!=null){
				CommunityBase community = communityBaseService.getById(report.getCommunityId());
				if(community!=null){
					report.setCommunityName(community.getName());
				}
			}
			UserBaseVo user = userBaseService.getById(report.getCreatorId());
			report.setCreatorName(user==null ? "" : user.getNick());
			
			if(BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(report.getBizzType())){//话题
				CircleTopic ct = circleTopicService.getById(report.getBizzId());
				userQuery.setId(ct.getCreatorId());
				List<UserBaseVo> userList = userBaseService.queryList4Admin(userQuery, pageKit);
				if(userList != null && userList.size() > 0){
					if(ct != null){
						report.setBizzUser(userList.get(0).getNick());
						report.setBizzAccount(userList.get(0).getUserIdentity());
						report.setBizzContent(ct.getContent());
						report.setBizzImg(ct.getImg());
					}	
				}
			}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(report.getBizzType())){ //大喇叭
				CommunityMessage cm = communityMessageService.getById(report.getBizzId());
				userQuery.setId(cm.getCreatorId());
				List<UserBaseVo> userList = userBaseService.queryList4Admin(userQuery, pageKit);
				if(userList != null && userList.size() > 0){
					if(cm != null){
						report.setBizzUser(userList.get(0).getNick());
						report.setBizzAccount(userList.get(0).getUserIdentity());
						report.setBizzContent(cm.getContent());
						report.setBizzImg(cm.getImg());
					}	
				}
			}
			model.addAttribute("report", report);
		}
		
		return "/report/report-detail";
	}

	/**
	 * 解除屏蔽
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/showReport")
	@ResponseBody
	public Result showReport(Integer reportId, String bussType, Integer bussId) {
		Result ret = new Result();
		pubReportService.handleIgnoreReport(super.getLoginUser().getId(), reportId, bussType, bussId);
		return ret;
	}

	/**
	 * 屏蔽
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/hideReport")
	@ResponseBody
	public Result hideReport(Integer reportId, String bussType, Integer bussId) {
		Result ret = new Result();
		String title = pubReportService.handleDisposeReport(super.getLoginUser().getId(), reportId, bussType, bussId);
		ret.put("bussTitle",title);
		return ret;
	}
	

	/** 
	 * 有效举报
	 * 
	 * @param user
	 */
	/*private void saveAndNotice(BehaviorTypeEnum bt,UserDomain user,Integer reportId,Integer bussType, Integer bussId,Map<String, Object> params) {
		UserBehaviorDTO bDomain = new UserBehaviorDTO();
		bDomain.setBussId(reportId);
		bDomain.setRootId(bussId);
		bDomain.setBussName("");
		bDomain.setBussContent(user.getUserName());
		bDomain.setBussType(bussType);
		bDomain.setCode(bt.getCode());
		bDomain.setUserId(user.getId());
		bDomain.setUserName(user.getUserName());
		if(params!=null){
			bDomain.setParams(params);
		}
		userBehaviorService.saveUserBehaviorToRedis(bDomain);
	}*/
}
